package com.shxt.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.shxt.mapper.CompanyMapper;
import com.shxt.model.Company;
import com.shxt.service.CompanyService;
@Service
public class CompanySerivceimpl implements CompanyService {

    @Autowired
    private CompanyMapper companyMapper;

    @Override
    public int delete(String company_ids ) {
        // TODO Auto-generated method stub
        String[] company_id = company_ids.split("-");
        int i =0;
        for(String idString : company_id){
            companyMapper.delete(Integer.parseInt(idString));
            i++;
        }

        
        return i;
    }
    
    @Override
    public int add(Company company,HttpServletRequest request) {
        // TODO Auto-generated method stub

        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");//设置日期格式
        


        HttpSession session = request.getSession();
        //从session获取创建人信息
        company.setCompany_user_name(session.getAttribute("userName").toString());
        company.setCompany_user_id(Integer.parseInt(session.getAttribute("userId").toString()));
        company.setCompany_create_time(df.format(new Date()));// new Date()为获取当前系统时间
        
        return companyMapper.add(company);
    }
    
    @Override
    public Company load(Integer company_id,HttpServletRequest request) {
        // TODO Auto-generated method stub
        HttpSession session = request.getSession();
        //将其存起，从详情页编辑时，直接读取
        session.setAttribute("company_id" , company_id);
        
        
        return companyMapper.load(company_id);
    }
    
    @Override
    public int update(Company company ) {
        // TODO Auto-generated method stub

        return companyMapper.update(company);
        
    }
    //查询所有数据以及全字段查询
    @Override
    public List<Company> listCompany(Map<String , Object> map) {
        // TODO Auto-generated method stub
        

        return companyMapper.listCompany(map);
    }
    
    @Override
    public Integer count(Map<String , Object> map ) {
        // TODO Auto-generated method stub
        return companyMapper.count(map);
    }
    
}
